package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author zhangyan
 * @date 2021/4/21 17:11
 */
@Service
@Transactional
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel>
        implements AdChannelService {

    /**
     * 根据频道名称分页查询频道列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //校验参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "参数不能为空");
        }

        //分页参数校验
        dto.checkParam();

        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper();

        //检查dto类name属性是否为空
        if (StringUtils.isNoneBlank(dto.getName())) {
            //不为空 拼接查询条件,模糊查询
            queryWrapper.like(AdChannel::getName, dto.getName());
        }

        //执行分页查询
        IPage<AdChannel> page = new Page<>(dto.getPage(), dto.getSize());
        page(page, queryWrapper);

        //返回数据
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());

        return responseResult;
    }

    /**
     * 新增频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //校验参数
        if (adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //添加必要字段的值
        adChannel.setIsDefault(true);
        adChannel.setCreatedTime(new Date());
        //执行插入
        save(adChannel);

        //相应数据
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 更新频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult update(AdChannel adChannel) {
        //参数校验
        if (adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //检查数据是否存在
        AdChannel channel = getById(adChannel.getId());
        if (channel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道不存在");
        }

        //执行更新
        updateById(adChannel);

        //相应数据
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        //参数校验
        if (id == null || id == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //查询频道是否存在
        AdChannel channel = getById(id);
        if (channel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道数据不存在");
        }
        //如果频道存在,判断状态是否可用
        if (channel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIC_ERROR,"频道数据启用,不能删除");
        }

        //执行删除
        removeById(id);

        //响应数据
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
